﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Skynet.CommonLibrary.ExceptionEx.Inteface;

namespace Skynet.CommonLibrary.ExceptionEx.Config
{
   internal class ReadConfig
    {
       internal static List<ExceptionConfigEntity> Read(out IExceptionDataSource datasource)
       {
           XElement root = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "\\ExceptionEx\\ExConfig.xml");
           XAttribute xAttribute = root.Attribute("ExDataSourceType");
           if (xAttribute != null)
           {
               string dbType = xAttribute.Value;
               if (dbType == string.Empty)
               {
                   throw new Exception("数据库操作类型不能为空");
               }
               //设置数据库操作类
               datasource = (IExceptionDataSource)System.Reflection.Assembly.Load(dbType.Split(',')[0]).CreateInstance(dbType.Split(',')[1]);
           }

           List<ExceptionConfigEntity> list = new List<ExceptionConfigEntity>();
          foreach (XElement element in root.Elements().Where(o => o.Name == "LogHandler"))
          {
              ExceptionConfigEntity configEntity = new ExceptionConfigEntity();
              XAttribute attribute = element.Attribute("Name");
              if (attribute != null) configEntity.Name = attribute.Value;
              //string logType = element.Attribute("Type").Value;
              //if (logType == string.Empty)
              //{
              //    throw new Exception("日志操作类型不能为空");
              //}
              //configEntity.LogHandler = (ILogHandler)System.Reflection.Assembly.Load(logType.Split(',')[0]).CreateInstance(logType.Split(',')[1]);
              XAttribute xAttribute1 = element.Attribute("ReturnMode");
              if (xAttribute1 != null)
                  configEntity.ReturnMode = (ExReturnMode)Enum.Parse(typeof(ExReturnMode), xAttribute1.Value);
              XAttribute attribute1 = element.Attribute("MsgMode");
              if (attribute1 != null)
                  configEntity.MsgMode = (ExShowMsgType)Enum.Parse(typeof(ExShowMsgType), attribute1.Value);
              if (configEntity.MsgMode != ExShowMsgType.None)
              {
                  XElement msgElement = element.Elements("MsgType").FirstOrDefault();
                  if (msgElement == null)
                      throw new Exception("错误消息输出类型不存在，请增加配置");
                  XAttribute xAttribute2 = msgElement.Attribute("Type");
                  if (xAttribute2 != null)
                  {
                      string msgType = xAttribute2.Value;
                      if (msgType == string.Empty)
                      {
                          throw new Exception("错误消息输出类型不能为空");
                      }
                      configEntity.OutMessage = (IOutMessage)System.Reflection.Assembly.Load(msgType.Split(',')[0]).CreateInstance(msgType.Split(',')[1]);
                  }
              }

              list.Add(configEntity);
          }

           datasource = null;
           return list;
       }
    }
}
